#include<iostream>
#include<string.h>
using std::cout;
using std::endl;
class Raii;

class Computer
{
public:
    Computer(const char* brand,float price)
        :_brand(new char[strlen(brand)+1]())
         ,_price(price)
    {
        strcpy(_brand,brand);
        cout<<"构造"<<endl;
    }
    friend
        class Raii;

    Computer& operator=(Computer& lhs)
    {
        if(this!=&lhs)
        {
            if(_brand)
            {
                delete[]_brand;
            }
                _brand=lhs._brand;
                lhs._brand=nullptr;
                _price=lhs._price;

        }
        return *this;
    }
    Computer& operator=(Computer&& lhs)
    {
        cout<<"移动赋值"<<endl;
        if(this!=&lhs)
        {
            if(_brand)
            {
                delete[]_brand;
            }
                _brand=lhs._brand;
                lhs._brand=nullptr;
                _price=lhs._price;

        }
        return *this;
    }
private:
    ~Computer()
    {
        if(_brand)
        {
            delete []_brand;
            _brand=nullptr;
        }
        cout<<"析构"<<endl;
    }
private:
    char* _brand;
    float _price;
};
class Raii
{
public:
    Raii(const char* brand,float price)
        :_compu(new Computer(brand,price))
    {
        //构造创建
        cout<<"raii 构造"<<endl;
    }
    ~Raii()
    {
        //析构释放
        if(_compu)
        {
            cout<<"真Raii析构"<<endl;

            delete _compu;
        }
    }

    //提供操作方法

    const char* getbrand()
    {
        return (*_compu)._brand;
    }

    float getprice()
    {
        return (*_compu)._price;
    }

    void print()
    {
        cout<<"("<<(*_compu)._brand<<" "<<(*_compu)._brand<<")"<<endl;
    }

    void reset(const char* lhs,float rhs)
    {
        *_compu=Computer(lhs,rhs);
        cout<<"raii reset"<<endl;
    }

private:
    Computer* _compu;
    //一般不允许赋值、复制
    Raii(const Computer& lhs)=delete;
    Raii& operator=(const Computer& hs)=delete;
};
int main()
{
    //应该设置成在main函数手动开辟，把Raii里面开辟流程去除
    //写作Raii rlease(new Computer(_brand,__price)
    Raii ra("dell",8000);
    ra.print();
    cout<<ra.getbrand()<<" brand"<<endl;
    cout<<ra.getprice()<<" price"<<endl;
}

